<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/13 0013
 * Time: 下午 6:45
 */
namespace app\api\controller;

use app\common\validate\ProductgetListValidate;
use think\facade\Request;

class Product extends Base{

    public function getCategory(){
        $list = db('product_category')->where('status',1)->select();
        if($list){
            return show(\Yaconf::get('status.success'),'ok',$list);
        }else{
            return show(\Yaconf::get('status.error'),'暂无数据',[]);
        }
    }

    public function getList(){
        (new ProductgetListValidate())->goCheck();
        $param = Request::param();
        $start = $this->start;
        $length = $this->length;
        if(!empty($param['start'])){
            $start =  $param['start'];
        }
        if(!empty($param['length'])){
            $length = $param['length'];
        }
        if($param['category'] == 0 && $param['type'] == 0){
            $where = [
                ['status','=',1]
            ];
        }else{
            if($param['category'] != 0 && $param['type'] == 0){
                $where = [
                    ['status','=',1],
                    ['category_id','=',$param['category']]
                ];
            }
        }
        $product = db('product')->where($where)->limit($start,$length)->select();
        $count = db('product')->where($where)->count();

        if($product){
            return showPage(\Yaconf::get('status.success'),'ok',$product,$count);
        }else{
            return show(\Yaconf::get('status.error'),'暂无数据',[]);
        }

    }
}